package net.dipper.tankwar.game.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import wnb.util.config.ConfigurationManager;

import net.dipper.tankwar.core.helper.GameFileManager;

public class OperationPane extends JPanel implements ActionListener, GameContextListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private MainWindow parent;
	private GameContext context;
	private GamePane gamePane;
	private JButton btnPause;
	private JButton btnSave;
	private JButton btnBack;
	
	public OperationPane(MainWindow parent, GameContext context){
		this.parent = parent;
		this.context = context;
		this.gamePane = new GamePane(context, 100, true);
		this.btnPause = new JButton("暂停游戏");
		this.btnSave = new JButton("保存游戏");
		this.btnBack = new JButton("返回菜单");
		JPanel toolPane = new JPanel();
		toolPane.setLayout(new BoxLayout(toolPane, BoxLayout.Y_AXIS));
		
		this.btnPause.setMaximumSize(new Dimension(200, 32));
		this.btnSave.setMaximumSize(new Dimension(200, 32));
		this.btnBack.setMaximumSize(new Dimension(200, 32));
		toolPane.add(Box.createRigidArea(new Dimension(10, 10)));
		toolPane.add(this.btnPause);
		toolPane.add(Box.createRigidArea(new Dimension(10, 10)));
		toolPane.add(this.btnSave);
		toolPane.add(Box.createRigidArea(new Dimension(10, 10)));
		toolPane.add(this.btnBack);
		toolPane.setPreferredSize(new Dimension(240, 600));
		
		this.btnPause.addActionListener(this);
		this.btnSave.addActionListener(this);
		this.btnBack.addActionListener(this);
		
		setLayout(new BorderLayout());
		add(gamePane, BorderLayout.CENTER);
		add(toolPane, BorderLayout.EAST);
		
		context.addListener(this);
		
		gamePane.start();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source.equals(btnPause)){
			pause();
		}else if(source.equals(btnSave)){
			save();
		}else if(source.equals(btnBack)){
			back();
		}
	}
	
	private void back(){
		MenuPane pane = new MenuPane(parent);
		parent.setPane(pane);
	}
	
	//保存游戏
	private void save() {
		context.pause();
		btnPause.setText("继续游戏");
		
		String fileName = JOptionPane.showInputDialog("请输入想要保存的文件名");
		if(fileName == null)
			return;
		
		fileName = fileName.trim();
		if("".equals(fileName)){
			JOptionPane.showMessageDialog(parent, "填写的文件名无效");
		}else{
			try {
				if(!fileName.endsWith("." + GameFileManager.EXT)){
					fileName += "." + GameFileManager.EXT;
				}
				
				String dir = ConfigurationManager.getAppSetting("dir_saves");
				File file = new File(dir, fileName);
				GameFileManager.save(context.getMap(), new FileOutputStream(file));
				JOptionPane.showMessageDialog(parent, "保存成功");
			} catch (ParserConfigurationException | SAXException | IOException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
			}
		}
	}

	//暂停
	private void pause(){
		if(context.getIsPaused()){
			context.resume();
			parent.requestFocus();
			btnPause.setText("暂停游戏");
		}else{
			context.pause();
			btnPause.setText("继续游戏");
		}
	}

	@Override
	public void onGameContextStatusChanged(GameContext context, byte status) {
		switch(status){
		case GameContext.STATUS_RUNNING:
			btnPause.setEnabled(true);
			btnSave.setEnabled(false);
			break;
			
		case GameContext.STATUS_PAUSED:
			btnPause.setEnabled(true);
			btnSave.setEnabled(true);
			break;
			
		case GameContext.STATUS_STOPED:
			btnPause.setEnabled(false);
			btnSave.setEnabled(false);
			break;
		}
	}
}
